// 获取应用实例
const app = getApp()

Page({

    data: {
        total_bom_id: 0,
        template_no: '',
        list_data: [],
        table_head: [
            {id: 12, name: 'bom名', field: 'bom_name', is_checked: true, width: 160, show_user: [0,1,2,3]},
            {id: 3, name: '需求数量', field: 'need_num', is_checked: true, width: 150, show_user: [0,1,2,3]},
            {id: 4, name: '采购数', field: 'total_num', is_checked: true, width: 130, show_user: [0,1,3]},
            {id: 11, name: '未采购数', field: 'no_caigou_num', is_checked: true, width: 130, show_user: [0,1]},
            {id: 5, name: '入库数', field: 'reviced_num', is_checked: true, width: 130, show_user: [0,2,3]},
            {id: 6, name: '未收数', field: 'less_num', is_checked: true, width: 130, show_user: [0,2,3]},
            {id: 7, name: '已发数', field: 'send_num', is_checked: true, width: 130, show_user: [0,3]},
            {id: 15, name: '未发数', field: 'send_less_num', is_checked: true, width: 130, show_user: [0,3]},
            {id: 8, name: '单价', field: 'price', is_checked: true, width: 130, show_user: [0]},
            {id: 9, name: '交期', field: 'dead_line', is_checked: true, width: 180, show_user: [0]},
            {id: 10, name: '采购时间', field: 'diy_cg_time', is_checked: true, width: 150, show_user: [0]},
        ],
        table_scroll_left: 0,
        table_user_type: 0,
    },

    onLoad(options) {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '【' + options.template_no + '】BOM清单'
        });
        // 获取数据
        if (options.total_bom_id != undefined) {
            this.setData({
                total_bom_id: options.total_bom_id,
                template_no: options.template_no,
                table_user_type: options.table_user_type == undefined ? 0 : options.table_user_type,
            });
            this.get_bom_list();
        }
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 获取bom详情
    get_bom_list: function () {
        var __this = this;
        // 请求参数
        var req_data = {
            total_bom_id: this.data.total_bom_id,
        };
        // 发起请求
        var result = app.request_api('admin/totalBom/getTotalBom', req_data);
        result.then(res => {
            console.log(res)
            // 验证显现字段
            this.data.table_head.forEach(function (item, index) {
                if (item.show_user.includes(parseInt(__this.data.table_user_type))) {
                    __this.data.table_head[index].is_checked = true;
                } else {
                    __this.data.table_head[index].is_checked = false;
                }
            })
            // 重设data
            this.setData({
                list_data: res.bom_list,
                table_head: this.data.table_head
            });
        })
    },



})